package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	//ctx, cancel := context.WithCancel(context.Background())
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	go Speak(ctx)
	time.Sleep(10 * time.Second)
}

func Speak(ctx context.Context) {
	/*for range time.Tick(time.Second) {
		select {
		case <-ctx.Done():
			fmt.Println(ctx.Err())
			return
		default:
			time.Sleep(time.Second * 2)
			fmt.Println("balabalabalabala")
		}
	}*/

	timer := time.NewTimer(time.Second * 1)
	for {
		select {
		case <-timer.C:
			fmt.Println("start")
			time.Sleep(time.Second * 6)
			fmt.Println("stop")
		case <-ctx.Done():
			fmt.Println(ctx.Err())
			return
		}
	}
}
